package com.hins.demo.gatewayimpl;

import com.hins.demo.gateway.ExchangeRateGateway;
import com.hins.demo.model.transfer.valueobject.Currency;
import com.hins.demo.model.transfer.valueobject.ExchangeRate;
import com.hins.demo.rpc.YahooForexMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class ExchangeRateGatewayImpl implements ExchangeRateGateway {

    @Autowired
    private YahooForexMapper yahooForexMapper;

    @Override
    public ExchangeRate getExchangeRate(Currency source, Currency target) {
        // 货币相等 不用汇率
        if (source.equals(target)) {
            return new ExchangeRate(BigDecimal.ONE, source, target);
        }
        // 不相等，去第三方渠道拿取转化后的汇率
        BigDecimal forex = yahooForexMapper.getExchangeRate(source.getValue(), target.getValue());

        return new ExchangeRate(forex, source, target);
    }
}